(* Content-type: application/vnd.wolfram.mathematica *)

(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)

(* CreatedBy='Mathematica 8.0' *)

(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[       157,          7]
NotebookDataLength[     15253,        398]
NotebookOptionsPosition[     14070,        356]
NotebookOutlinePosition[     14415,        371]
CellTagsIndexPosition[     14372,        368]
WindowFrame->Normal*)

(* Beginning of Notebook Content *)
Notebook[{

Cell[CellGroupData[{
Cell["J-K latch", "Section",
 CellChangeTimes->{{3.520880011078125*^9, 3.520880015078125*^9}, {
  3.520938836109375*^9, 3.52093884040625*^9}, {3.5295848125*^9, 
  3.529584814875*^9}, {3.52959633665625*^9, 3.52959634803125*^9}, {
  3.636612021283645*^9, 3.636612026330934*^9}}],

Cell[BoxData[
 RowBox[{"<<", "C:\\\\Hopsan\\Compgen\\CompgenNG.mx"}]], "Input",
 CellChangeTimes->{{3.5772037650720577`*^9, 3.5772037650780582`*^9}, {
  3.611474457184202*^9, 3.611474523938877*^9}, {3.6114745588913717`*^9, 
  3.61147456057154*^9}}],

Cell[BoxData[
 RowBox[{
  RowBox[{"path", "=", 
   RowBox[{"ToFileName", "[", 
    RowBox[{"{", 
     RowBox[{
     "\"\<C:\>\"", ",", "\"\<HopsanTrunk\>\"", ",", 
      "\"\<ComponentLibraries\>\"", ",", "\"\<defaultLibrary\>\"", ",", 
      "\"\<Signal\>\"", ",", "\"\<Logic\>\""}], "}"}], "]"}]}], 
  ";"}]], "Input",
 CellChangeTimes->{{3.5757727777685804`*^9, 3.575772792541806*^9}, {
   3.5757728423954945`*^9, 3.5757728498367076`*^9}, {3.57577333888545*^9, 
   3.5757733706315055`*^9}, 3.5757734316206255`*^9, 3.6031909973287797`*^9, 
   3.6057717887213297`*^9, {3.605771829393656*^9, 3.605771830929744*^9}, 
   3.6058530990363216`*^9, {3.621138851005574*^9, 3.621138863716845*^9}, {
   3.626686112671085*^9, 3.6266861209310846`*^9}, {3.632581474573143*^9, 
   3.632581481776555*^9}, {3.6363469199835668`*^9, 3.636346925752144*^9}}],

Cell[CellGroupData[{

Cell["System model", "Subsection",
 CellChangeTimes->{{3.504161099890625*^9, 3.504161102453125*^9}}],

Cell[BoxData[{
 RowBox[{
  RowBox[{"domain", "=", "\"\<Signal\>\""}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"displayName", "=", "\"\<JKlatch\>\""}], ";"}], "\n", 
 RowBox[{
  RowBox[{"brief", "=", "\"\<J-K latch\>\""}], ";"}], "\n", 
 RowBox[{
  RowBox[{"componentType", "=", "\"\<ComponentSignal\>\""}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"author", "=", "\"\<Petter Krus <petter.krus@liu.se>\>\""}], 
  ";"}], "\n", 
 RowBox[{
  RowBox[{
  "affiliation", " ", "=", " ", 
   "\"\<Division of Fluid and Mechatronic Systems, Link\[ODoubleDot]ping \
University\>\""}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"SetFilenames", "[", 
   RowBox[{"path", ",", "domain", ",", "displayName"}], "]"}], 
  ";"}]}], "Input",
 CellChangeTimes->{{3.520880152296875*^9, 3.520880230078125*^9}, 
   3.520932125875*^9, 3.520932753984375*^9, {3.52093324790625*^9, 
   3.520933251453125*^9}, 3.522342790078125*^9, {3.52958482121875*^9, 
   3.5295848575625*^9}, {3.529588367046875*^9, 3.5295883780625*^9}, {
   3.529590218*^9, 3.5295902181875*^9}, {3.529596354421875*^9, 
   3.529596369015625*^9}, 3.5764735069894204`*^9, {3.636612031966256*^9, 
   3.6366120390306606`*^9}}],

Cell[BoxData[
 RowBox[{
  RowBox[{"ResetComponentVariables", "[", "]"}], ";"}]], "Input"],

Cell[CellGroupData[{

Cell["Definition of behaviour", "Text",
 CellGroupingRules->{GroupTogetherGrouping, 10000.},
 CellChangeTimes->{{3.6366352810920305`*^9, 3.6366352982320104`*^9}, 
   3.636635562443123*^9}],

Cell[BoxData[
 RowBox[{
  RowBox[{"GridBox", "[", 
   RowBox[{
    RowBox[{"{", "\[IndentingNewLine]", 
     RowBox[{
      RowBox[{"{", 
       RowBox[{
       "\"\<J(set)\>\"", ",", "\"\<K(reset)\>\"", ",", "\"\<Qold\>\"", ",", 
        "\"\<Q\>\""}], "}"}], ",", "\[IndentingNewLine]", 
      RowBox[{"{", 
       RowBox[{"0", ",", "0", ",", "\"\<Qold\>\"", ",", "\"\<Qold\>\""}], 
       "}"}], ",", "\[IndentingNewLine]", 
      RowBox[{"{", 
       RowBox[{"0", ",", "1", ",", "\"\<x\>\"", ",", "0"}], "}"}], ",", 
      "\[IndentingNewLine]", 
      RowBox[{"{", 
       RowBox[{"1", ",", "0", ",", "\"\<x\>\"", ",", "1"}], "}"}], ",", 
      "\[IndentingNewLine]", 
      RowBox[{"{", 
       RowBox[{"1", ",", "1", ",", "\"\<Qold\>\"", ",", "\"\<Not[Qold]\>\""}],
        "}"}]}], "}"}], ",", 
    RowBox[{"GridBoxDividers", "\[Rule]", 
     RowBox[{"{", 
      RowBox[{
       RowBox[{"\"\<Rows\>\"", "\[Rule]", 
        RowBox[{"{", 
         RowBox[{"{", "True", "}"}], "}"}]}], ",", 
       RowBox[{"\"\<Columns\>\"", "\[Rule]", 
        RowBox[{"{", 
         RowBox[{"{", "True", "}"}], "}"}]}]}], "}"}]}]}], "]"}], "//", 
  "DisplayForm"}]], "Input",
 CellGroupingRules->{GroupTogetherGrouping, 10000.},
 CellChangeTimes->{{3.636558593504751*^9, 3.636558675431437*^9}, {
   3.6365587112074833`*^9, 3.6365587580161605`*^9}, 3.6365588561917763`*^9, {
   3.6365588929248767`*^9, 3.6365589301220045`*^9}, {3.6365589654940276`*^9, 
   3.6365590026771545`*^9}, {3.6365590354800305`*^9, 3.636559104718991*^9}, {
   3.636559141335085*^9, 3.6365591458683443`*^9}, {3.636559176767112*^9, 
   3.636559197155278*^9}, {3.6365592414628124`*^9, 3.6365592459710703`*^9}, {
   3.636634387817938*^9, 3.636634418710705*^9}, {3.636634481812314*^9, 
   3.6366345079978113`*^9}, {3.636634576261716*^9, 3.636634590335521*^9}, {
   3.636634797460368*^9, 3.63663480432176*^9}, {3.6366349031764145`*^9, 
   3.636634935165244*^9}, {3.636634969094185*^9, 3.636634980529839*^9}, {
   3.6366350627845435`*^9, 3.6366351748879557`*^9}, {3.6366354231191535`*^9, 
   3.6366354434953194`*^9}, 3.636635562443123*^9}]
}, Closed]],

Cell[BoxData[
 TagBox[GridBox[{
    {
     RowBox[{"J", 
      RowBox[{"(", "set", ")"}]}], 
     RowBox[{"K", 
      RowBox[{"(", "reset", ")"}]}], "Qold", "Q"},
    {"0", "0", "Qold", "Qold"},
    {"0", "1", "x", "0"},
    {"1", "0", "x", "1"},
    {"1", "1", "Qold", 
     RowBox[{"Not", "[", "Qold", "]"}]}
   },
   GridBoxDividers->{"Columns" -> {{True}}, "Rows" -> {{True}}}],
  DisplayForm]], "Output",
 CellChangeTimes->{3.636635138086851*^9, 3.636635176270035*^9, 
  3.6366354498196807`*^9, 3.636635564580245*^9}]
}, Open  ]],

Cell[CellGroupData[{

Cell["Variables and parameters", "Subsection"],

Cell[BoxData[
 RowBox[{
  RowBox[{"inputVariables", ":=", " ", 
   RowBox[{"{", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"{", 
      RowBox[{
      "setCond", ",", "0.", ",", "double", ",", "\"\<\>\"", ",", 
       "\"\<On condition\>\""}], "}"}], ",", "\[IndentingNewLine]", 
     RowBox[{"{", 
      RowBox[{
      "resetCond", ",", "0.", ",", "double", ",", "\"\<\>\"", ",", 
       "\"\<off condition\>\""}], "}"}]}], "\n", "}"}]}], ";"}]], "Input",
 CellChangeTimes->{{3.520880034703125*^9, 3.520880034703125*^9}, {
   3.520932825078125*^9, 3.520932825328125*^9}, 3.529585849328125*^9, {
   3.529596531265625*^9, 3.529596537390625*^9}, {3.529650349909154*^9, 
   3.529650351237194*^9}}],

Cell[BoxData[
 RowBox[{
  RowBox[{"outputVariables", ":=", " ", 
   RowBox[{"{", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"{", 
      RowBox[{
      "Qstate", ",", "0.", ",", "double", ",", "\"\<\>\"", ",", 
       "\"\<Logical state\>\""}], "}"}], ",", "\[IndentingNewLine]", 
     RowBox[{"{", 
      RowBox[{
      "notQstate", ",", "1.", ",", "double", ",", "\"\<\>\"", ",", 
       "\"\<Logical inverse of state\>\""}], "}"}]}], "\n", "}"}]}], 
  ";"}]], "Input",
 CellChangeTimes->{{3.52088004696875*^9, 3.520880046984375*^9}, 
   3.520932825859375*^9, {3.52958588634375*^9, 3.529585902890625*^9}, {
   3.52959659559375*^9, 3.529596615265625*^9}, 3.5296472601147585`*^9, {
   3.5296473407948947`*^9, 3.5296473473268137`*^9}, 3.529649459263307*^9, 
   3.590904738943883*^9, {3.6363935683614626`*^9, 3.636393572588705*^9}}],

Cell["Defintion of logical functions using float variables", "Text",
 CellChangeTimes->{3.6366352320592256`*^9}],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"Andf", "[", 
    RowBox[{"u1_", ",", "u2_"}], "]"}], ":=", 
   RowBox[{"limit", "[", 
    RowBox[{
     RowBox[{"u1", "*", "u2"}], ",", "0", ",", "1"}], "]"}]}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"Orf", "[", 
    RowBox[{"u1_", ",", "u2_"}], "]"}], ":=", 
   RowBox[{"limit", "[", 
    RowBox[{
     RowBox[{"2", 
      RowBox[{"(", 
       RowBox[{
        RowBox[{"onPositive", "[", 
         RowBox[{"u1", "-", "0.5"}], "]"}], "+", 
        RowBox[{"onPositive", "[", 
         RowBox[{"u1", "-", "0.5"}], "]"}]}], ")"}]}], ",", "0", ",", "1"}], 
    "]"}]}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"Nandf", "[", 
    RowBox[{"u1_", ",", "u2_"}], "]"}], ":=", 
   RowBox[{"1", "-", 
    RowBox[{"limit", "[", 
     RowBox[{
      RowBox[{"u1", "*", "u2"}], ",", "0", ",", "1"}], "]"}]}]}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"Norf", "[", 
    RowBox[{"u1_", ",", "u2_"}], "]"}], ":=", 
   RowBox[{"1", "-", 
    RowBox[{"limit", "[", 
     RowBox[{
      RowBox[{"2", 
       RowBox[{"(", 
        RowBox[{
         RowBox[{"onPositive", "[", 
          RowBox[{"u1", "-", "0.5"}], "]"}], "+", 
         RowBox[{"onPositive", "[", 
          RowBox[{"u1", "-", "0.5"}], "]"}]}], ")"}]}], ",", "0", ",", "1"}], 
     "]"}]}]}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"Notf", "[", 
    RowBox[{"u1_", ",", "u2_"}], "]"}], ":=", 
   RowBox[{"1", "-", 
    RowBox[{"limit", "[", 
     RowBox[{"u1", ",", "0", ",", "1"}], "]"}]}]}], ";"}]}], "Input",
 CellChangeTimes->{{3.636610276874871*^9, 3.6366104189869995`*^9}, {
  3.6366105304093723`*^9, 3.6366106489881544`*^9}, {3.636610727319635*^9, 
  3.636610766086852*^9}, {3.6366108111324286`*^9, 3.6366108387260065`*^9}}]
}, Open  ]],

Cell[CellGroupData[{

Cell["The system of equations", "Subsection"],

Cell[BoxData[
 RowBox[{
  RowBox[{"initialExpressions", "=", 
   RowBox[{"{", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"oldQstate", "==", "Qstate"}], ",", "\[IndentingNewLine]", 
     RowBox[{"oldSetCond", "==", "setCond"}], ",", "\[IndentingNewLine]", 
     RowBox[{"oldResetCond", "==", "resetCond"}]}], "\[IndentingNewLine]", 
    "}"}]}], ";"}]], "Input",
 CellChangeTimes->{{3.52234700896875*^9, 3.52234701828125*^9}, {
   3.52234704890625*^9, 3.522347131203125*^9}, {3.522347193828125*^9, 
   3.522347242390625*^9}, {3.529587134296875*^9, 3.52958716409375*^9}, 
   3.529587754421875*^9, 3.5295963953125*^9, {3.529647256989439*^9, 
   3.5296472580207944`*^9}, {3.529647337935227*^9, 3.529647353718093*^9}, {
   3.5296872766875*^9, 3.529687338453125*^9}, {3.529687383921875*^9, 
   3.5296873858125*^9}, {3.5719247270915413`*^9, 3.571924745717974*^9}, 
   3.5719252640444846`*^9}],

Cell[BoxData[
 RowBox[{
  RowBox[{"expressions", "=", 
   RowBox[{"{", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"Qstate", "\[Equal]", 
      RowBox[{"limit", "[", 
       RowBox[{
        RowBox[{
         RowBox[{"Andf", "[", 
          RowBox[{"oldQstate", ",", 
           RowBox[{"Andf", "[", 
            RowBox[{
             RowBox[{"Notf", "[", "setCond", "]"}], ",", 
             RowBox[{"Notf", "[", "resetCond", "]"}]}], "]"}]}], "]"}], 
         "\[IndentingNewLine]", "-", 
         RowBox[{"Andf", "[", 
          RowBox[{
           RowBox[{"Notf", "[", "setCond", "]"}], ",", "resetCond"}], "]"}], 
         "\[IndentingNewLine]", "+", 
         RowBox[{"Andf", "[", 
          RowBox[{"setCond", ",", 
           RowBox[{"Notf", "[", "resetCond", "]"}]}], "]"}], 
         "\[IndentingNewLine]", "+", 
         RowBox[{"Andf", "[", 
          RowBox[{
           RowBox[{"Andf", "[", 
            RowBox[{"setCond", ",", "resetCond"}], "]"}], ",", 
           RowBox[{"Notf", "[", "oldQstate", "]"}]}], "]"}]}], ",", "0", ",", 
        "1"}], "]"}]}], ",", "\[IndentingNewLine]", 
     RowBox[{"oldQstate", "\[Equal]", "Qstate"}], ",", "\[IndentingNewLine]", 
     RowBox[{"notQstate", "==", 
      RowBox[{"1", "-", "Qstate"}]}]}], "\[IndentingNewLine]", "}"}]}], 
  ";"}]], "Input",
 CellChangeTimes->{{3.529586025328125*^9, 3.52958604828125*^9}, {
   3.529586463703125*^9, 3.5295865311875*^9}, {3.5295869555*^9, 
   3.52958704159375*^9}, {3.529587082453125*^9, 3.52958712321875*^9}, {
   3.529587709234375*^9, 3.52958771875*^9}, {3.52958824928125*^9, 
   3.529588250875*^9}, {3.52958830328125*^9, 3.529588310671875*^9}, {
   3.529596403*^9, 3.52959645915625*^9}, {3.529596510671875*^9, 
   3.529596527171875*^9}, {3.5295965615625*^9, 3.529596585640625*^9}, {
   3.5296472501293616`*^9, 3.5296472536140933`*^9}, {3.52964732004277*^9, 
   3.52964737482963*^9}, {3.5296486047915845`*^9, 3.529648605072869*^9}, 
   3.529650357502418*^9, {3.5296505716074257`*^9, 3.5296505771857653`*^9}, {
   3.529687265359375*^9, 3.529687267546875*^9}, {3.529687403515625*^9, 
   3.529687484203125*^9}, {3.529687527171875*^9, 3.52968755115625*^9}, {
   3.5297491187115407`*^9, 3.529749138767107*^9}, {3.571924759679999*^9, 
   3.571924782643239*^9}, 3.571925072491748*^9, {3.577602445774715*^9, 
   3.577602447837715*^9}, 3.5908985542438164`*^9, {3.5908986701958165`*^9, 
   3.590898684410816*^9}, {3.590898999878015*^9, 3.590899024486476*^9}, {
   3.6363468055924115`*^9, 3.6363468121424117`*^9}, {3.636610874239038*^9, 
   3.6366109242749*^9}, {3.636611208105134*^9, 3.636611223961041*^9}, {
   3.636611306029735*^9, 3.6366113280269933`*^9}, 3.6366113766517744`*^9, {
   3.636611523069149*^9, 3.636611562532406*^9}, {3.6366116267910814`*^9, 
   3.6366118184350433`*^9}, {3.636611892094256*^9, 3.6366119016178007`*^9}, 
   3.6366124142671223`*^9, {3.636612923662258*^9, 3.6366129242002892`*^9}, {
   3.636613001334701*^9, 3.6366130145724583`*^9}, 3.6366130549477673`*^9, {
   3.6366132336599894`*^9, 3.6366132886321335`*^9}, 3.6366133413441486`*^9, {
   3.636613408250975*^9, 3.6366134355655375`*^9}, {3.6366135716713223`*^9, 
   3.636613572347361*^9}, {3.6366144519816732`*^9, 3.6366144581690273`*^9}}],

Cell[BoxData[
 RowBox[{"Compgen", "[", "file", "]"}]], "Input"]
}, Open  ]]
}, Open  ]]
},
WindowSize->{779, 740},
WindowMargins->{{159, Automatic}, {Automatic, -54}},
FrontEndVersion->"8.0 for Microsoft Windows (64-bit) (October 6, 2011)",
StyleDefinitions->"Default.nb"
]
(* End of Notebook Content *)

(* Internal cache information *)
(*CellTagsOutline
CellTagsIndex->{}
*)
(*CellTagsIndex
CellTagsIndex->{}
*)
(*NotebookFileOutline
Notebook[{
Cell[CellGroupData[{
Cell[579, 22, 276, 4, 71, "Section"],
Cell[858, 28, 248, 4, 31, "Input"],
Cell[1109, 34, 839, 16, 31, "Input"],
Cell[CellGroupData[{
Cell[1973, 54, 100, 1, 36, "Subsection"],
Cell[2076, 57, 1195, 28, 152, "Input"],
Cell[3274, 87, 89, 2, 31, "Input"],
Cell[CellGroupData[{
Cell[3388, 93, 188, 3, 29, "Text",
 CellGroupingRules->{GroupTogetherGrouping, 10000.}],
Cell[3579, 98, 2096, 44, 132, "Input",
 CellGroupingRules->{GroupTogetherGrouping, 10000.}]
}, Closed]],
Cell[5690, 145, 522, 16, 111, "Output"]
}, Open  ]],
Cell[CellGroupData[{
Cell[6249, 166, 46, 0, 36, "Subsection"],
Cell[6298, 168, 700, 16, 92, "Input"],
Cell[7001, 186, 836, 18, 92, "Input"],
Cell[7840, 206, 112, 1, 29, "Text"],
Cell[7955, 209, 1827, 56, 112, "Input"]
}, Open  ]],
Cell[CellGroupData[{
Cell[9819, 270, 45, 0, 36, "Subsection"],
Cell[9867, 272, 892, 16, 112, "Input"],
Cell[10762, 290, 3214, 59, 172, "Input"],
Cell[13979, 351, 63, 1, 31, "Input"]
}, Open  ]]
}, Open  ]]
}
]
*)

(* End of internal cache information *)
